package com.mxy.myapplication2.java;

import android.content.Context;
import android.util.Log;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2017/11/8.
 *
 * @author 马晓勇
 */

public class Person implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final String TAG = "MXY";
    private ReentrantLock lock = new ReentrantLock(false);

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        goodsNums.add(new GoodsNum("小浩浩", 50));
    }

    public String name;

    public int age;

    public String sex = "大肚子";

    public ArrayList<GoodsNum> goodsNums = new ArrayList<>();

    public class GoodsNum implements Serializable {

        public GoodsNum(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String name;
        public int age;
    }

    /**
     * 在异步线程中访问
     */
    public void testLockA(Context context) {
        if (context.getMainLooper().getThread().getId() == Thread.currentThread().getId()) {
            Log.i(TAG, "不能在主线程中运行");
            return;
        }
        lock.lock();
        try {
            Log.i(TAG, "testLockA begin");
            Thread.sleep(2 * 1000);
            Log.i(TAG, "testLockA end");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 在异步线程中访问
     */
    public void testLockB(Context context) {
        if (context.getMainLooper().getThread().getId() == Thread.currentThread().getId()) {
            Log.i(TAG, "不能在主线程中运行");
            return;
        }
        lock.lock();
        try {
            Log.i(TAG, "testLockB begin");
            Thread.sleep(2 * 1000);
            Log.i(TAG, "testLockB end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 在异步线程中访问
     */
    public static synchronized void testSynchronizedA(Context context) {
        if (context.getMainLooper().getThread().getId() == Thread.currentThread().getId()) {
            Log.i(TAG, "不能在主线程中运行");
            return;
        }
        try {
            Log.i(TAG, "testSynchronizedA begin");
            Thread.sleep(2 * 1000);
            Log.i(TAG, "testSynchronizedA end");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 在异步线程中访问
     */
    public static synchronized void testSynchronizedB(Context context) {
        if (context.getMainLooper().getThread().getId() == Thread.currentThread().getId()) {
            Log.i(TAG, "不能在主线程中运行");
            return;
        }
        try {
            Log.i(TAG, "testSynchronizedB begin");
            Thread.sleep(2 * 1000);
            Log.i(TAG, "testSynchronizedB end");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}